<?php

namespace app\admin\controller;

use app\common\controller\Base;

class Commodity extends Base
{
    /**
     * 显示资源列表
     */
    public function index()
    {
        $page = input('get.page',1);
        $numPage = 6;
        $startPage = ($page-1) * $numPage;
        $data = db('commodity')->limit($startPage,$numPage)->select();
        $count = db('commodity')->count();
        $pages = ceil(($count/$numPage));
        $data = editTime($data);
        foreach ($data as $k=>$v){
            $image = explode('|',$v['image']);
            $data[$k]['image'] = array_shift($image);
        }
        $this->assign('page',$page);
        $this->assign('pages',$pages);
        $this->assign('data',$data);
        $this->assign('count',$count);
        return view();
    }

    /**
     * 显示编辑资源表单页.
     */
    public function edit()
    {
        $id = input('get.id/d','');
        $info = db('ccolumn')->field('id,title')->select();
        $data = db('commodity')->where('id', $id)->find();
        $image = explode('|',$data['image']);
        $this->assign('image', $image);
        $this->assign('data', $data);
        $this->assign('info', $info);
        $this->assign('id', $id);
        return view();
    }

    /**
     * 保存新建的资源
     */
    public function save()
    {
        $data['id'] = input('post.id/d','');
        $data['name'] = input('post.name/s','');
        $data['name'] || jump_page('','商品名称不能为空!',2);
        db('commodity')->where('id','NOT IN',$data['id'])->where(['name'=>$data['name']])->find() && jump_page('','该商品名称目已存在!',2);
        $data['hid'] = input('post.hid/d','','/^[0-9]*$/');
        $data['hid'] || jump_page('','商品货号必须为数字!',2);
        db('commodity')->where('id','NOT IN',$data['id'])->where(['hid'=>$data['hid']])->find() && jump_page('','该商品货号目已存在!',2);
        $data['cid'] = input('post.cid/d','');
        $data['cid'] || jump_page('','商品栏目不能为空!',2);
        $data['details'] = input('post.details/s','','/([\x{4e00}-\x{9fa5}]+)/u');
        $data['details'] || jump_page('','商品介绍必须为汉字!',2);
        $data['price'] = input('post.price/d','','/^[0-9]*$/');
        $data['price'] || jump_page('','商品价格必须为数字!',2);
        $data['stock'] =  input('post.stock/d','','/^[0-9]*$/');
        $data['stock'] || jump_page('','商品库存必须为数字!',2);
        if($data['id']){
            $file = $_FILES;
            if(!$file){
                unset($data["image"]);
            }else{
                foreach ($file as $k => $v){
                    if($v['name']){
                        $num[] = substr($k, -1);
                        $path[] = update_image($k,4000000,'data','/static/imgs/');
                    };
                }
                $imgs = db('commodity')->field('image')->where('id', $data['id'])->find();
                $imgs = explode('|',$imgs['image']);
                foreach ($path as $k => $v){
                    $img[] = $imgs[$num[$k]];
                    $imgs[$num[$k]] = $v;
                }
                $img = implode('|',$img);
                $info[]['image'] = $img;
                delete_images('commodity','',$info);
                $data['image'] = implode('|',$imgs);
            }
            $commodity = db('commodity')->where('id', $data['id'])->update($data);
            return $commodity !== false ? _layer_msg('更新成功!','',1) : jump_page('','更新失败!',2);
        }
        $data["image"] = update_images('img',4000000,'date','/static/imgs/','admin/Commodity/index');
        $data["image"] || jump_page('','请上传图片!',2);
        $data['image'] = implode('|',$data['image']);
        $data['addTime'] = time();
        $datas = db('commodity')->insert($data);
        return $datas ? _layer_msg('已添加!','admin/Commodity/index',6) : _layer_msg('添加失败!','admin/Commodity/index');
    }

    /**
     * 删除指定资源
     */
    public function delete()
    {
        $ids = input('post.id','');
        delete_images('commodity',$ids);
        if(!$ids) return json(['status'=>0,'msg'=>'请选择要删除的角色!']);
        return db('commodity')->delete($ids) ? json(['status'=>1,'msg'=>'已删除!']) : json(['status'=>0,'msg'=>'删除失败!']);
    }

    /**
     * 改变商品状态
     */
    public function status(){
        $data['id'] = input('post.id/d','');
        $data['status'] = input('post.status/d','');
        if(!$data['id']) return json(['status'=>0,'msg'=>'请选择要更新的状态!']);
        if($data['status'] == 1){
            $data['status'] = 2;
            return db('commodity')->update($data) ? json(['status'=>1,'msg'=>'已审核']) : json(['status'=>0,'msg'=>'该操作暂不可执行!']);
        }
        if($data['status'] == 2){
            $data['status'] = 3;
            return db('commodity')->update($data) ? json(['status'=>2,'msg'=>'发布']) : json(['status'=>0,'msg'=>'该操作暂不可执行!']);
        }
        if($data['status'] == 3){
            $data['status'] = 4;
            return db('commodity')->update($data) ? json(['status'=>3,'msg'=>'已发布']) : json(['status'=>0,'msg'=>'该操作暂不可执行!']);
        }
        if($data['status'] == 4){
            $data['status'] = 3;
            return db('commodity')->update($data) ? json(['status'=>4,'msg'=>'下架']) : json(['status'=>0,'msg'=>'该操作暂不可执行!']);
        }

    }
}
